package com.gt.aspect;

import com.gt.contant.AjaxCode;
import com.gt.contant.AjaxResult;
import com.gt.contant.CmdCodeException;
import com.gt.contant.CommonAjaxResult;
import com.gt.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @功能说明：全局异常类处理
 * @作者： 黄和润
 * @创建日期：2021/8/25
 * @版本号：V1.0
 */
@ControllerAdvice
@Slf4j
public class MyExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public AjaxResult exceptionHandler(Exception e) {
        AjaxResult ajaxResult = new CommonAjaxResult(AjaxCode.SERVICE_ERROR);
        log.error("全局异常日志--->",e);
        return ajaxResult;
    }

    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public AjaxResult exceptionHandler(ServiceException e) {
        log.error("业务异常", e);
        //统一的异常信息格式
        return new CommonAjaxResult(e.getAjaxCode().getCode(), StringUtils.isEmpty(e.getMessage()) ? e.getAjaxCode().getMessage() : e.getMessage());
    }

    @ExceptionHandler(value = CmdCodeException.class)
    @ResponseBody
    public AjaxResult codeExceptionHandler(CmdCodeException e) {
        log.error("短信发送异常", e);
        //统一的异常信息格式
        return new CommonAjaxResult(e.getAjaxCode().getCode(), StringUtils.isEmpty(e.getMessage()) ? e.getAjaxCode().getMessage() : e.getMessage());
    }
}
